//This code was created with the help of Cairngorm Creator by Tyler Beck.
//The portions of this document created by Cairngorm Creator 
//are provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
//CONDITIONS OF ANY KIND, either express or implied.
//========================================================



package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetUserEvent;
	import net.optix.ip.model.ModelLocator;
	import net.optix.ip.vo.UserVO;
	import net.optix.ip.vo.ClientVO;
	import net.optix.ip.vo.InventorVO;
	import net.optix.ip.vo.FirmVO;
	import net.optix.ip.vo.PractitionerVO;
	import views.ViewInstance;

	public class GetUserCommand implements Command, Responder
	{
		// attributes ============================
		private var model:ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			
			var myEvent:GetUserEvent = GetUserEvent( event );
			
			var delegate:UserDelegate = new UserDelegate();     
			delegate.getUser( this,myEvent.userID );
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			try
			{
				trace(event.result);
				var user:XML	=	event.result;
				
				model.client	=	ClientVO.purseClient(user.client[0]);
				model.inventor	=	InventorVO.purseInventor(user.inventor[0]);
				model.firm		=	FirmVO.purseFirm(user.firm[0]);
				model.practitioner=	PractitionerVO.pursePractitioner(user.practitioner[0]);
				model.user	=	UserVO.purseUser(user);
				if(model.role > 0 && model.role < 5)
				{
					if(model.view == ViewInstance.Welcome)
					{
						model.view		=	0;
						model.view		=	ViewInstance.ViewInventions;
					}
					
				}
				switch(model.role)
				{
					case 1:
						model.userID	=	model.client.clientID;
						break;
					case 2:
						model.userID	=	model.inventor.inventorID;
						break;
					case 3:
						model.userID	=	model.firm.firmID;
						break;
					case 4:
						model.userID	=	model.practitioner.practitionerID;
						break;
				}
			}
			catch(error:Error)
			{	
			}
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			
		}

	}
}